import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';

class BaseTeamuserOdooApi {
  static Future<ResponseBodyApi> page({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(
        domain: domainList,
        limit: pageModel.size,
        offset: (pageModel.current - 1) * pageModel.size,
      );

      var records = await OdooHttpUtil.callKw(
          model: 'hr.employee',
          method: 'search_read',
          kwargs: recordsKwargs.toMap());
      //var total = 106;
      var total = await OdooHttpUtil.callKw(
          model: 'hr.employee',
          method: 'search_count',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      print(e);
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> alreadyPage({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
          model: 'base.teamuser',
          method: 'Refactor_data',
          //  fieldsInfo: fieldsInfo,
          kwargs: recordsKwargs.toMap());

      var total = await OdooHttpUtil.callKw(
          model: 'base.teamuser',
          method: 'Data_Statistics',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      print(e);
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> postraioList({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,);
      var records = await OdooHttpUtil.callKw(
          model: 'base.teamuser',
          method: 'Refactor_data',
          //  fieldsInfo: fieldsInfo,
          kwargs: recordsKwargs.toMap());
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      responseBodyApi = ResponseBodyApi(success: true, data: records);
    } catch (e) {
      print(e);
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> userList({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs already = Kwargs();
      var records = await OdooHttpUtil.callKw(
          model: 'base.teamuser',
          method: 'search_read',
          kwargs: already.toMap());
      //kwargs: already.toMap());
      List<dynamic> useridLIst = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        useridLIst.add(map['staff_id'][0]);
      });
      responseBodyApi = ResponseBodyApi(success: true, data: useridLIst);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> removeByIds(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'base.teamuser', method: "unlink", args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> saveOrUpdate(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'base.teamuser', method: 'create', args: [data]);

      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> batchUpdateUser(List data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'hr.employee', method: 'postratio_write', args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }
}
